#!/usr/bin/env python
#coding = utf-8

from flask import Flask, request, jsonify
from flask_cors import CORS
from pyproj import Proj, transform
#import rospy
#from std_msgs.msg import UInt32

app = Flask(__name__)
CORS(app)

pub = None

@app.route('/waypoint', methods=['GET', 'POST'])
def waypoint():
    global pub
    # 获取参数 'number'
    """ number_str = request.args.get('number') or request.form.get('number') or (request.get_json() or {}).get('number')
    print (number_str) """

    print("Received POST request for /waypoint")
    try:
        # 尝试获取 JSON 数据
        data = request.get_json()

        # 检查是否成功获取到 JSON 数据
        if data is None:
            print("Error: No JSON data received or invalid JSON format")
            # 返回 400 Bad Request 错误
            return jsonify({"status": "error", "message": "Request body must be valid JSON"}), 400

        print(f"Successfully received JSON data: {data}")
        # 假设处理成功，返回成功响应
        return jsonify({"status": "success", "message": "Waypoint data received and processed", "received_data": data}), 200

    except Exception as e:
        # 捕获其他可能的处理错误
        print(f"Error processing /waypoint request: {e}")
        return jsonify({"status": "error", "message": "Internal server error"}), 500

# wgs是无人机使用的经纬度，gcj是高德地图的经纬度
def wgs84_to_gcj02(lat, lon):
    # 定义WGS-84坐标系
    wgs84 = Proj(init='epsg:4326')
    # 定义GCJ-02坐标系
    gcj02 = Proj(init='epsg:4550')

    # 转换坐标
    gcj_lat, gcj_lon = transform(wgs84, gcj02, lon, lat)
    return gcj_lat, gcj_lon

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=5001,debug=True)